<?php
namespace app\admin\controller;
use think\Request;
use think\View;
use think\Controller;
use app\admin\model\Room;
use app\admin\model\Wuser;
/**
 * 
 * @author 时
 * @description：创建教室控制器
 *
 */
class Rom extends Common{
 /**
     * @description 教室创建(包含专辑中扣出一部分创建教室)
     */
    
    public function create()
    {
     if(Request::instance()->isGet()){
         return  view('room_create');
     }else
     {
        var_dump(input('post.'));
     }
    }
    /**
     * @description 根据节点创建教室
     */
    
    public function create_node_room()
    {
     if(Request::instance()->isGet())
     {
         return view('node_add');
     }else 
     {
         //接受传递的post数据
         $title=input('post.title');
         $courseware_introduce=input('post.courseware_introduce');
         $node_id=input('post.node_id');
         $curriculum_time=input('post.curriculum_time');//如果用户没有填写就是当前时间
         
         $curriculum_time=empty($curriculum_time)?date("Y-m-d H:i"):$curriculum_time;
       
         $status=input('post.status');
         $end_time=input('post.end_time');//如果结束时间未填写默认当前时间+多久
         $end=strtotime($curriculum_time);
         $end_time=empty($end_time)?date('Y-m-d H:i',$end+3600):$end_time;
         $fee=input('post.fee');
         $user_id='179';//session中取出用户id
         
         
         //这里的user_id是get传递过来 或者session中获取或者memcached中
         
          
         //根据用户id查询数据库获取到网易云账号
         $wuser=new Wuser();
         $acid=$wuser->getacidByUserid($user_id)['acid'];
         
         
          
         //----------------调用网易云创建教室如果创建成功保存入库并且输入的验证成功
         $wxyiyun=new \wyiyun\Common(config('appkey'), config('appsecret'));
         $options=array(
             'name'=>$title,//聊天室名称，长度限制128个字符
             'creator'=>$acid,
             'announcement'=>'',
             'broadcasturl'=>''
         );
         $data=$wxyiyun->CreateChatrom($options);
         $data=json_decode($data,true);
         
         if($data['code']==200)
         {
             //创建教室成功获得room_id
              
             $room_id=$data['chatroom']['roomid'];
             $data_arr=['user_id'=>1,
                 'title'=>$title,
                 'courseware_introduce'=>$courseware_introduce,
                 'node_id'=>$node_id,
                 'curriculum_time'=>$curriculum_time,
                 'status'=>$status,
                 'end_time'=>$end_time,
                 'fee'=>$fee,
                 'from'=>4,
                 'room_id'=>$room_id
             ];
             $RoomModel=new Room();
             $result=$RoomModel->validate(
                 [
                     'title'=>'require|max:128',
                     'courseware_introduce'=>'require|max:255',
                     'end_time'=>'require',
                     'fee'=>'require'
                 ],
                 [
                     'title.require'=>'标题名称必须填写',
                     'tile.max'=>'标题最多不超过128个字符',
                     'courseware_introduce.require'=>'课程介绍必须填写',
                     'courseware_introduce.max'=>'课程介绍最大不超过255个字符',
                     'end_time.require'=>'课程结束时间必须填写',
                     'fee.require'=>'课程收费必须填写'
                 ]
                 )->save($data_arr);
                 if(false===$result)
                 {
                     $this->error($RoomModel->getError());
                 }else {
                     $this->success('创建教室成功');
                 }
         
         }else {
             $this->error('网易云教室创建失败');
         }
          
         //进行保存到数据库
     }
    }
    public function show()
    {
        $view=new View();
        return $view->fetch('index');
    }
    /*
     * @description 根据专辑创建教室
     * @return null
     */
    public function create_alum_room()
    {
      if(Request::instance()->isGet())
      {
         return view('alum_add');
      }else 
      {
        //接受传递的post数据  
        
       $title=input('post.title');
       $courseware_introduce=input('post.courseware_introduce');
       $album_id=input('post.album_id');
       $curriculum_time=input('post.curriculum_time');
       $curriculum_time=empty($curriculum_time)?date("Y-m-d H:i"):$curriculum_time;
       $status=input('post.status');
       $end_time=input('post.end_time');
       $end=strtotime($curriculum_time);
       $end_time=empty($end_time)?date('Y-m-d H:i',$end+3600):$end_time;
       $fee=input('post.fee');
       $user_id='179';//session中取出用户id
      
       
       //这里的user_id是get传递过来 或者session中获取或者memcached中
      
       
       //根据用户id查询数据库获取到网易云账号
       $wuser=new Wuser();
       $acid=$wuser->getacidByUserid($user_id)['acid'];
 
      
       
       //----------------调用网易云创建教室如果创建成功保存入库并且输入的验证成功
       $wxyiyun=new \wyiyun\Common(config('appkey'), config('appsecret'));
       $options=array(
           'name'=>$title,//聊天室名称，长度限制128个字符
           'creator'=>$acid,
           'announcement'=>'',
           'broadcasturl'=>''
       );
       $data=$wxyiyun->CreateChatrom($options);
       $data=json_decode($data,true);
  
      if($data['code']==200)
      {
         //创建教室成功获得room_id 
         
         $room_id=$data['chatroom']['roomid'];
         $data_arr=['user_id'=>1,
             'title'=>$title,
             'courseware_introduce'=>$courseware_introduce,
             'album_id'=>$album_id,
             'curriculum_time'=>$curriculum_time,
             'status'=>$status,
             'end_time'=>$end_time,
             'fee'=>$fee,
             'from'=>5,
             'room_id'=>$room_id
         ];
         $RoomModel=new Room();
         $result=$RoomModel->validate(
             [
                 'title'=>'require|max:128',
                 'courseware_introduce'=>'require|max:255',
                 'end_time'=>'require',
                 'fee'=>'require'
             ],
             [
                 'title.require'=>'标题名称必须填写',
                 'tile.max'=>'标题最多不超过128个字符',
                 'courseware_introduce.require'=>'课程介绍必须填写',
                 'courseware_introduce.max'=>'课程介绍最大不超过255个字符',
                 'end_time.require'=>'课程结束时间必须填写',
                 'fee.require'=>'课程收费必须填写'
             ]
             )->save($data_arr);
             if(false===$result)
             {
                 $this->error($RoomModel->getError());
             }else {
                 $this->success('创建教室成功');
             }
        
      }else {
          $this->error('网易云教室创建失败');
      }
     
       //进行保存到数据库
      
       
      }
    }
    
    /**
     * @description:根据我的专辑页面传递专辑id,创建教室
     * @param string $albu_id 专辑ID
     * @return NULL
     * 
     */
    public function crealuromByalum($albu_id='23')
    {
        if(Request::instance()->isGet()){
            return view('my_room_alum');
        }else 
        {
            //接受传递的post数据
            
            $title=input('post.title');
            $courseware_introduce=input('post.courseware_introduce');
            $album_id=$albu_id;
            $curriculum_time=input('post.curriculum_time');
            $curriculum_time=empty($curriculum_time)?date("Y-m-d H:i"):$curriculum_time;
            $status=input('post.status');
            $end_time=input('post.end_time');
            $end=strtotime($curriculum_time);
            $end_time=empty($end_time)?date('Y-m-d H:i',$end+3600):$end_time;
            $fee=input('post.fee');
            $user_id='179';//session中取出用户id
            
             
            //这里的user_id是get传递过来 或者session中获取或者memcached中
            
             
            //根据用户id查询数据库获取到网易云账号
            $wuser=new Wuser();
            $acid=$wuser->getacidByUserid($user_id)['acid'];
            
            
             
            //----------------调用网易云创建教室如果创建成功保存入库并且输入的验证成功
            $wxyiyun=new \wyiyun\Common(config('appkey'), config('appsecret'));
            $options=array(
                'name'=>$title,//聊天室名称，长度限制128个字符
                'creator'=>$acid,
                'announcement'=>'',
                'broadcasturl'=>''
            );
            $data=$wxyiyun->CreateChatrom($options);
            $data=json_decode($data,true);
            
            if($data['code']==200)
            {
                //创建教室成功获得room_id
                 
                $room_id=$data['chatroom']['roomid'];
                $data_arr=['user_id'=>1,
                    'title'=>$title,
                    'courseware_introduce'=>$courseware_introduce,
                    'album_id'=>$album_id,
                    'curriculum_time'=>$curriculum_time,
                    'status'=>$status,
                    'end_time'=>$end_time,
                    'fee'=>$fee,
                    'from'=>5,
                    'room_id'=>$room_id
                ];
                $RoomModel=new Room();
                $result=$RoomModel->validate(
                    [
                        'title'=>'require|max:128',
                        'courseware_introduce'=>'require|max:255',
                        'end_time'=>'require',
                        'fee'=>'require'
                    ],
                    [
                        'title.require'=>'标题名称必须填写',
                        'tile.max'=>'标题最多不超过128个字符',
                        'courseware_introduce.require'=>'课程介绍必须填写',
                        'courseware_introduce.max'=>'课程介绍最大不超过255个字符',
                        'end_time.require'=>'课程结束时间必须填写',
                        'fee.require'=>'课程收费必须填写'
                    ]
                    )->save($data_arr);
                    if(false===$result)
                    {
                        $this->error($RoomModel->getError());
                    }else {
                        $this->success('创建教室成功');
                    }
            
            }else {
                $this->error('网易云教室创建失败');
            }
             
            //进行保存到数据库
        }
        
    }
    /**
     * @description:创建教室根据我的节点页面 选择传递节点 list_ids 列表创建节点教室
     * @param string $node_ids 节ID列表
     * @return null;
     */
    public function creanoderomBynode($node_ids="1,2,3")
    {
        //这里传递过来的最好是 1/2/3 或者1,2,3这样的字符串
        if(Request::instance()->isGet()){
            return  view('my_room_node');
        }else 
        {
            //接受传递的post数据
            $title=input('post.title');
            $courseware_introduce=input('post.courseware_introduce');
            $node_id=$node_ids;
            $curriculum_time=input('post.curriculum_time');
            $curriculum_time=empty($curriculum_time)?date("Y-m-d H:i"):$curriculum_time;
            $status=input('post.status');
            $end_time=input('post.end_time');
            $end=strtotime($curriculum_time);
            $end_time=empty($end_time)?date('Y-m-d H:i',$end+3600):$end_time;
            $fee=input('post.fee');
            $user_id='179';//session中取出用户id
             
             
            //这里的user_id是get传递过来 或者session中获取或者memcached中
             
            
            //根据用户id查询数据库获取到网易云账号
            $wuser=new Wuser();
            $acid=$wuser->getacidByUserid($user_id)['acid'];
             
             
            
            //----------------调用网易云创建教室如果创建成功保存入库并且输入的验证成功
            $wxyiyun=new \wyiyun\Common(config('appkey'), config('appsecret'));
            $options=array(
                'name'=>$title,//聊天室名称，长度限制128个字符
                'creator'=>$acid,
                'announcement'=>'',
                'broadcasturl'=>''
            );
            $data=$wxyiyun->CreateChatrom($options);
            $data=json_decode($data,true);
             
            if($data['code']==200)
            {
                //创建教室成功获得room_id
            
                $room_id=$data['chatroom']['roomid'];
                $data_arr=['user_id'=>1,
                    'title'=>$title,
                    'courseware_introduce'=>$courseware_introduce,
                    'node_id'=>$node_id,
                    'curriculum_time'=>$curriculum_time,
                    'status'=>$status,
                    'end_time'=>$end_time,
                    'fee'=>$fee,
                    'from'=>4,
                    'room_id'=>$room_id
                ];
                $RoomModel=new Room();
                $result=$RoomModel->validate(
                    [
                        'title'=>'require|max:128',
                        'courseware_introduce'=>'require|max:255',
                        'end_time'=>'require',
                        'fee'=>'require'
                    ],
                    [
                        'title.require'=>'标题名称必须填写',
                        'tile.max'=>'标题最多不超过128个字符',
                        'courseware_introduce.require'=>'课程介绍必须填写',
                        'courseware_introduce.max'=>'课程介绍最大不超过255个字符',
                        'end_time.require'=>'课程结束时间必须填写',
                        'fee.require'=>'课程收费必须填写'
                    ]
                    )->save($data_arr);
                    if(false===$result)
                    {
                        $this->error($RoomModel->getError());
                    }else {
                        $this->success('创建教室成功');
                    }
                     
            }else {
                $this->error('网易云教室创建失败');
            }
            
        }
    }
    /**
     * 创建自定义教室 
     */
    public function create_coustom_room()
    {
       //在线创建白板 通过发出请求
    }
   
   
    public function test(){
      // session('user_id', 'thinkphp');
//         $wyiyun=new \wyiyun\Common(config("appkey"), config("appsecret"));
//         $data=json_decode($wyiyun->check_accid("s222"),true);
//         if($data['msg']=="register"){
//             echo "账号已经同步注册过";
//         }else {
//             //进行调用方法同步
//             echo "账号同步成功";
//         }
//         $wyiyun=new \wyiyun\Common(config('appkey'),config('appsecret'));
//       $data=$wyiyun->syctoaccount('w2er222');
//       $data=json_decode($data,true);
//       var_dump($data);
       
       // $WuserModel=new Wuser();
     //  $WuserModel->acid=$data['info']['accid'];
//        $WuserModel->user_id='223';
//        $WuserModel->name="ss";
       //$WuserModel->token=$data['info']['token'];
       //$WuserModel->save();
//         var_dump($WuserModel);
//         $WuserModel->acid=$data['info']->accid;
//         $WuserModel->token=$data['info']->token;
//         $WuserModel->name=$data['info']->name;
//         $WuserModel->user_id=$user_account;
//         $WuserModel->save();
    }
function add_node_room()
{
    return view('add_node');
}
function ex(){
    return view('ex');
}
function sh(){
    return view('add');
}
    
}